
package uestc.bitman.edxapp.tools;

/**
 * @author hackeris
 * @category Some functions of preprocess the json and xml
 */
public class ResponseStringTools {

    /**
     * @param s json to be processed
     * @return processed json String
     * @category Remove some character incorrect in json
     */
    public static String preProcJson(String s) {

        String str = s;// new String(s);
        int i, ct = -1;
        boolean b = false;
        for (i = 0; i < str.length(); i++) {

            if ((str.charAt(i) >= '!') && (str.charAt(i) <= '~')) {

                if (str.charAt(i) == '}') {
                    b = true;
                    continue;
                }
                if ((str.charAt(i) == ',') && (b)) {
                    ct = i;
                    continue;
                }
                if ((str.charAt(i) == ']') && (b) && (ct != -1)) {
                    str = str.substring(0, ct) + str.substring(ct + 1);
                }
                b = false;
                ct = -1;
            }
        }
        return str.replace("\n", "");
    }

    /**
     * @param s the xml to be processed
     * @return processed xml String
     * @category Replace some the special code in xml
     */
    public static String preProcXml(String s) {

        String str;
        str = s.replace("&amp;", "&");
        str = str.replace("&lt;", "<");
        str = str.replace("&#34;", "\"");
        str = str.replace("&gt;", ">");
        str = str.replace("&#39;", "\'");
        str = str.replace("&nbsp;", " ");
        return str;
    }
}
